package com.weavernorth.action.humanresource;


import com.weavernorth.humanresource.SalarRangeUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import sun.misc.BASE64Encoder;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.io.IOException;
import java.util.Iterator;

/**
 * 员工自助审批流推送SAP
 */
public class EmployeeSelfAction extends BaseBean implements Action {
    private static Log log = LogFactory.getLog(EmployeeSelfAction.class);

    /**
     * 流程路径节点后选择aciton后,会在节点提交后执行此方法。
     */
    @Override
    public String execute(RequestInfo request) {
        log.info("进入员工自助action:"+request.getRequestid());
        //数据库操作
        RecordSet rs = new RecordSet();
        RecordSet mbrs=new RecordSet();
        //配置表查询
        RecordSet rsSelect = new RecordSet();
        //请求id
        String strRequestid = request.getRequestid();
        //表名
        String strTable = request.getRequestManager().getBillTableName();
        //基础类
        BaseBean bb=new BaseBean();
        //工具类
        SalarRangeUtil salarRangeUtil = new SalarRangeUtil();
        //推送SAP 的xml拼接
        StringBuffer strRequestXml = new StringBuffer();
        strRequestXml.append("<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/' xmlns:urn='urn:sap-com:document:sap:soap:functions:mc-style'>");

        strRequestXml.append("<soapenv:Header/>");
        strRequestXml.append("<soapenv:Body>");
        strRequestXml.append("<urn:Zhri083>");
        //主表id
        String strMainid = "";
        //查询主表sql
        String strSql = " select * from " + strTable + " where requestid=?";
        rs.executeQuery(strSql, strRequestid);
        if (rs.next()) {
            strRequestXml.append("<IFamst></IFamst>");

            strMainid = Util.null2String(rs.getString("id"));
            //申请人
            String strSqr = Util.null2String(rs.getString("sqr"));
            //申请人编码
            String strSqrCode = salarRangeUtil.getWorkcode(strSqr);
            strRequestXml.append("<IPernr>" + strSqrCode + "</IPernr>");
            //10建模页面,20流程页面
            strRequestXml.append("<IType>20</IType>");
            //户口所在地
            String strHkszd = Util.null2String(rs.getString("hkdz"));
            strRequestXml.append("<IGbort>" + strHkszd + "</IGbort>");

            //户口类型
            String strHklx = Util.null2String(rs.getString("hklx"));
            //户口类型SAP
            String strHklxSap = "";
            if ("0".equals(strHklx)) {
                strHklxSap = "2";
            } else if ("1".equals(strHklx)) {
                strHklxSap = "1";
            } else if ("2".equals(strHklx)) {
                strHklxSap = "3";
            }
            strRequestXml.append("<IHukot>" + strHklxSap + "</IHukot>");
            strRequestXml.append("<IUsrid></IUsrid>");
            strRequestXml.append("<IZxxdz></IZxxdz>");


            //银行
            String strYh = Util.null2String(rs.getString("yxmc"));
            String strYhmc = ""; //银行名称
           String  mbsql = "select  yxdm  from  uf_yhmc  where id = ?";
            mbrs.executeQuery(mbsql,strYh);
            //银行卡号
            String strYhkh = Util.null2String(rs.getString("yxkh"));
            //银行卡生效日期
            String strSxrq = Util.null2String(rs.getString("sxrq"));
            //最早参加工作日期
            String strCjgzrq = Util.null2String(rs.getString("cjgzrq"));
            strRequestXml.append("<IBankdat>" + strSxrq + "</IBankdat>");
            strRequestXml.append("<IBankl>" + strYhmc + "</IBankl>");
            strRequestXml.append("<IBankn>" + strYhkh + "</IBankn>");
            strRequestXml.append("<IDate>" + strCjgzrq + "</IDate>");
            strRequestXml.append("<IZlxys></IZlxys>");
            strRequestXml.append("<IZxm></IZxm>");

            //region [2021-04-12：新增字段jrjtrq,jrgsrq]

            //进集团日期
            String strJtrq = Util.null2String(rs.getString("jrjtrq"));
            //进公司日期
            String strGsrq = Util.null2String(rs.getString("jrgsrq"));
            strRequestXml.append("<IJtrq>" + strJtrq + "</IJtrq>");
            strRequestXml.append("<IGsrq>" + strGsrq + "</IGsrq>");

            //endregion

        }

        strRequestXml.append("<Return>");

        strRequestXml.append("<item>");
        strRequestXml.append("<Type></Type>");
        strRequestXml.append("<Id></Id>");
        strRequestXml.append("<Number></Number>");
        strRequestXml.append("<Message></Message>");
        strRequestXml.append(" <LogNo></LogNo>");
        strRequestXml.append("<LogMsgNo></LogMsgNo>");
        strRequestXml.append("<MessageV1></MessageV1>");
        strRequestXml.append("<MessageV2></MessageV2>");
        strRequestXml.append("<MessageV3></MessageV3>");
        strRequestXml.append("<MessageV4></MessageV4>");
        strRequestXml.append("<Parameter></Parameter>");
        strRequestXml.append("<Row></Row>");
        strRequestXml.append("<Field></Field>");
        strRequestXml.append("<System></System>");
        strRequestXml.append("</item>");
        strRequestXml.append("</Return>");
        //明细表2学历信息
        if (!"".equals(strMainid)) {
            strRequestXml.append("<T_0022>");
            String strDetailSql = "select * from " + strTable + "_dt2 where mainid=?";
            rs.executeQuery(strDetailSql, strMainid);
            while (rs.next()) {
                //开始日期
                String strKsrq = Util.null2String(rs.getString("ksrq"));

                //毕业日期
                String strByrq = Util.null2String(rs.getString("byrq"));

                //学历
                String strXl = Util.null2String(rs.getString("xl"));
                String strXlSap = "";
                rsSelect.executeQuery("select  xlbm  from  uf_xl  where id=?", strXl);
                if (rsSelect.next()) {
                    strXlSap = Util.null2String(rsSelect.getString("xlbm"));
                }


                //院校|培训机构
                String strYxpxjg = Util.null2String(rs.getString("yxpxjg"));
                //院校性质
                String strYxxz = Util.null2String(rs.getString("yxxz"));
                //院校性质SAP
                String strYxxzSap = "";
                if ("0".equals(strYxxz)) {
                    strYxxzSap = "010";
                } else if ("1".equals(strYxxz)) {
                    strYxxzSap = "020";
                }
                //学制
                String strXz = Util.null2String(rs.getString("xz"));
                //所学专业
                String strSxzy = Util.null2String(rs.getString("sxzy"));
                //最高学历标识
                String strZgxlbs = Util.null2String(rs.getString("zgxlbs"));
                //学位
                String strXw = Util.null2String(rs.getString("xw"));
                //学位SAP
                String strXwSAP = "";
                if ("0".equals(strXw)) {
                    strXwSAP = "10";
                } else if ("1".equals(strXw)) {
                    strXwSAP = "01";
                }else if ("2".equals(strXw)) {
                    strXwSAP = "00";
                }else if ("3".equals(strXw)) {
                    strXwSAP = "52";
                }


                //最高学位标识
                String strZgxwbs = Util.null2String(rs.getString("zgxwbs"));


                strRequestXml.append("<item>");
                strRequestXml.append("<Slart>" + strXlSap + "</Slart>");
                strRequestXml.append("<Zxlks>"+strKsrq+"</Zxlks>");
                strRequestXml.append("<Zxljs>"+strByrq+"</Zxljs>");
                strRequestXml.append("<Insti>" + strYxpxjg + "</Insti>");
                strRequestXml.append("<Insat>" + strYxxzSap + "</Insat>");
                strRequestXml.append("<Sland></Sland>");
                strRequestXml.append("<Anzkl>" + strXz + "</Anzkl>");
                strRequestXml.append("<Fach3>" + strSxzy + "</Fach3>");
                strRequestXml.append("<Slabs>" + strXwSAP + "</Slabs>");
                strRequestXml.append("<Acaqm>" + strZgxlbs + "</Acaqm>");
                strRequestXml.append("<Acqid></Acqid>");
                strRequestXml.append("<Acaqd></Acaqd>");
                strRequestXml.append("<Accid></Accid>");
                strRequestXml.append("<Acacd></Acacd>");
                strRequestXml.append("<Acacm>" + strZgxwbs + "</Acacm>");
                strRequestXml.append("</item>");

            }
            strRequestXml.append("</T_0022>");


        }


        strRequestXml.append("<T_9124>");
        strRequestXml.append("<item>");
        strRequestXml.append(" <Zcygx></Zcygx>");
        strRequestXml.append("<Zxm></Zxm>");
        strRequestXml.append("<Zxb></Zxb>");
        strRequestXml.append("<Zlxys></Zlxys>");
        strRequestXml.append("<Zxgzfw></Zxgzfw>");
        strRequestXml.append("<Zdate></Zdate>");
        strRequestXml.append("</item>");
        strRequestXml.append("</T_9124>");

        strRequestXml.append("</urn:Zhri083>");
        strRequestXml.append("</soapenv:Body>");
        strRequestXml.append("</soapenv:Envelope>");
        log.info("流程员工自助推送SAP请求xml:" + strRequestXml);
        String strUrl = Util.null2String(bb.getPropValue("personnel-erdos","ygzzurl"));
        String strResponse = "";
        try {
            strResponse = salarRangeUtil.sendPost(strUrl, strRequestXml.toString(), "utf-8");
            log.info("SAP返回结果:" + strResponse);
            Document doc = null;//解析返回值开始
            int intType = 0;
            String fhz="";
            boolean bolFlag=true;
            String strMessage="";

            doc = DocumentHelper.parseText(strResponse);
            Iterator<Element> its2 = doc.selectNodes("//Type").iterator();
            while (its2.hasNext()) {
                Element element = its2.next();
                fhz = element.getText();
                if ("E".equals(fhz)) {
                    bolFlag = false;
                    break;
                }
                intType++;
            }

            int intMess = 0;

            //返回值失败时,则提示失败
            if ("E".equals(fhz)) {
                Iterator<Element> its3 = doc.selectNodes("//Message").iterator();
                while (its3.hasNext()) {
                    Element element2 = its3.next();
                    strMessage = element2.getText();
                    log.info("intMess:" + intMess + ",intType:" + intType);
                    if (intMess == intType) {
                        if (!"".equals(strMessage)) {
                            break;
                        }
                    }

                    intMess++;
                }
            }

            if (bolFlag) {
                return SUCCESS;
            } else {
                if (!"".equals(strMessage)) {
                    request.getRequestManager().setMessagecontent(strMessage);
                }
                return FAILURE_AND_CONTINUE;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return SUCCESS;
    }


}
